package com.student.education.controller;

import com.student.education.common.CommonResult;
import com.student.education.entity.Clazz;
import com.student.education.service.ClazzService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 班级维护表
 */
@RestController
@RequestMapping("class")
public class ClazzController {
    @Resource
    private ClazzService clazzService;

    /**
     *  查询所有班级
     */
    @PostMapping("queryAll")
    public CommonResult<List<Clazz>> queryAll(@RequestBody Clazz vo) {    //vo是入参，只有name和grade

        List<Clazz> clazzList = clazzService.queryAll(vo);
        return CommonResult.success(clazzList);
    }

    //增加
    @PostMapping("add")
    public CommonResult<String> add(@RequestBody Clazz vo) {
        int i = clazzService.add(vo);
        if (Objects.equals(i, 1)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.error("添加失败：存在相同班级");
    }


    //删除
    @GetMapping("delete")
    public CommonResult<String> delete(@RequestParam Integer id) {
        int i = clazzService.delete(id);
        if (Objects.equals(i, 1)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.error("删除失败");
    }

    //修改
    @PostMapping("update")
    public CommonResult<String> update(@RequestBody Clazz vo) {
        int i = clazzService.update(vo);
        if (Objects.equals(i, 1)) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.error("修改失败");
    }

}
